package com.qzy.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qzy.reggie.common.PageResult;
import com.qzy.reggie.mapper.EmployeeMapper;
import com.qzy.reggie.pojo.Employee;
import com.qzy.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public PageResult queryEmployeeList(Integer page, Integer pageSize, String name) {
        // 使用Mybatis的PageHelper插件完成分页功能

        // 1.通过插件，设置分页信息
        PageHelper.startPage(page,pageSize);

        // 2.正常实现员工列表查询（此时不需要考虑分页）
        List<Employee> employees = employeeMapper.selectList(name);

        // 3.封装PageInfo对象，获取分页total
        PageInfo<Employee> pageInfo = new PageInfo<>(employees);

        // 最后封装分页结果对象
        return new PageResult(pageInfo.getTotal(),employees);
    }

    @Override
    public Employee queryEmployeeById(Long id) {
        if(id == null){
            return null;
        }
        Employee emp = new Employee();
        emp.setId(id);
        return employeeMapper.selectOne(emp);
    }

    @Override
    public Employee queryEmployeeByUserName(String username) {
        if (username == null || "".equals(username)){
            return null;
        }
//        return employeeMapper.selectByUsername(username);

        Employee emp = new Employee();
        emp.setUsername(username);
        return employeeMapper.selectOne(emp);
    }

    @Override
    public Integer save(Employee employee) {

        return employeeMapper.insert(employee);
    }

    @Override
    public Integer update(Employee employee) {
        return employeeMapper.update(employee);
    }
}
